package com.yuyou.fn.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.ClassQuestionnaireRecord;
import com.yuyou.fn.educational.entity.Questionnaire;
import com.yuyou.fn.educational.entity.QuestionnaireQuestion;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.educational.vo.*;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.util.Webs;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wens on 2017/4/16.
 */
@Controller
@RequestMapping("/api/admin/questionnaire")
public class AdminQuestionnaireController extends ApiBaseController {

    @Resource
    private IQuestionnaireSettingService questionnaireSettingService;

    @Resource
    private IQuestionnaireService questionnaireService;

    @Resource
    private IQuestionnaireQuestionService questionnaireQuestionService;

    @Resource
    private IClassQuestionnaireRecordService classQuestionnaireRecordService;

    @Resource
    private IClassExpandDetailService classExpandDetailService;

    @ApiOperation("调研设置列表")
    @RequestMapping(value = "/findSettingPage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<NaireSettingVo>> findSettingPage(@ModelAttribute NaireSettingQueryVo naireSettingQueryVo, @RequestParam(value = "pageNo") int pageNo, @RequestParam(value = "pageSize")int pageSize) {
        Page<NaireSettingVo> questionnaireSettingPage=questionnaireSettingService.findPageByQueryVo(naireSettingQueryVo,pageNo,pageSize);
        return ReturnResult.success(questionnaireSettingPage) ;
    }

    @ApiOperation("保存或更新调研设置")
    @RequestMapping(value = "/saveOrUpdateSetting", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateSetting(@ModelAttribute NaireSettingVo naireSettingVo) {
        questionnaireSettingService.saveOrUpdate(naireSettingVo);
        return ReturnResult.success("ok") ;
    }

    @ApiOperation("调研详情")
    @RequestMapping(value = "/naireSettingVoDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<NaireSettingVo> saveOrUpdateSetting(@RequestParam("settingId")long settingId) {
        NaireSettingVo naireSettingVo = questionnaireSettingService.findOneBySettingId(settingId);
        return ReturnResult.success(naireSettingVo) ;
    }

    @ApiOperation("删除调研设置")
    @RequestMapping(value = "/deleteBySetting", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteBySetting(@RequestParam(value = "settingId")long settingId) {
        questionnaireSettingService.deleteBySettingId(settingId);
        return ReturnResult.success("ok") ;
    }

    @ApiOperation("调研设置选择问卷")
    @RequestMapping(value = "/settingSelectQuestionnaire", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> settingSelectQuestionnaire(@RequestParam(value = "settingId")long settingId,@RequestParam(value = "questionnaireId")long questionnaireId) {
        questionnaireSettingService.setUpQuestionnaire(settingId,questionnaireId);
        return ReturnResult.success("ok") ;
    }

    @ApiOperation("问卷列表")
    @RequestMapping(value = "/questionnaireList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<Questionnaire>> questionnaireList(@ModelAttribute QuestionnaireQueryVo questionnaireQueryVo, @RequestParam(value = "pageNo") int pageNo, @RequestParam(value = "pageSize")int pageSize) {
        Page<Questionnaire> questionnairePage=questionnaireService.findPageByQueryVo(questionnaireQueryVo,pageNo,pageSize);
        return ReturnResult.success(questionnairePage) ;
    }

    @ApiOperation("问卷新增编辑")
    @RequestMapping(value = "/saveUpdateQuestionnaire", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveUpdateQuestionnaire(@ModelAttribute QuestionnaireSaveUpdateVo questionnaireSaveUpdateVo) {
        questionnaireService.saveOrUpdate(questionnaireSaveUpdateVo);
        return ReturnResult.success("ok") ;
    }

    @ApiOperation("问卷删除")
    @RequestMapping(value = "/deleteQuestionnaire", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteQuestionnaire(@RequestParam("questionnaireId")long questionnaireId) {
        questionnaireService.deleteByQuestionnaireId(questionnaireId);
        return ReturnResult.success("ok") ;
    }

    @ApiOperation("问卷录入题目")
    @RequestMapping(value = "/importQuestion", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> importQuestion(@RequestParam(value = "questionnaireId")long questionnaireId,@RequestParam("questionListJson")String questionListJson) {
        List<QuestionnaireQuestion> list=JSONObject.parseArray(questionListJson,QuestionnaireQuestion.class);
        questionnaireQuestionService.setUpListByQuestionnaireId(questionnaireId,list);
        return ReturnResult.success("ok") ;
    }

    @ApiOperation("问卷题目列表,可用于预览，导出")
    @RequestMapping(value = "/questionList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<QuestionnaireQuestion>> questionList(@RequestParam(value = "questionnaireId")long questionnaireId) {
        List<QuestionnaireQuestion> questionnaireQuestionList = questionnaireQuestionService.findListByQuestionnaireId(questionnaireId);
        return ReturnResult.success(questionnaireQuestionList) ;
    }

    @ApiOperation("问卷详情")
    @RequestMapping(value = "/questionnaireDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Questionnaire> questionDetail(@RequestParam(value = "questionnaireId")long questionnaireId) {
        Questionnaire questionnaire=questionnaireService.findById(questionnaireId);
        return ReturnResult.success(questionnaire) ;
    }

    @ApiOperation("根据班级得到问卷详情（权重题目等）")
    @RequestMapping(value = "/questionListByClassId", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<QuestionnaireDetailVo> questionListByClassId(@RequestParam(value = "classId")long classId) {
        QuestionnaireDetailVo questionnaireDetail = questionnaireService.findClassQuestionnaireDetail(classId);
        return ReturnResult.success(questionnaireDetail) ;
    }


    @ApiOperation("录入班级问卷详情")
    @RequestMapping(value = "/saveQuestionnaireRecord", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<QuestionnaireDetailVo> saveQuestionnaireRecord(@ModelAttribute QuestionnaireRecordSaveVo questionnaireRecordSaveVo,@RequestParam("classQuestionDetailJson")String classQuestionDetailJson) {
        List<ClassQuestionSaveVo> list=JSONObject.parseArray(classQuestionDetailJson,ClassQuestionSaveVo.class);
        classQuestionnaireRecordService.saveRecord(questionnaireRecordSaveVo,list);
        return ReturnResult.success("ok") ;
    }

    @ApiOperation("班级问卷结果列表")
    @RequestMapping(value = "/questionnaireRecordList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ClassQuestionnaireRecord>> questionnaireRecordList(HttpServletRequest request,@ModelAttribute QuestionnaireRecordQueryVo questionnaireRecordQueryVo,@RequestParam(value = "pageNo") int pageNo,@RequestParam("pageSize") int pageSize) {
        if (questionnaireRecordQueryVo.getEndDate()!=null){
            questionnaireRecordQueryVo.setEndDate(TimeUtils.changeOneDateLastTime(questionnaireRecordQueryVo.getEndDate()));
        }
        DataPermission dataPermission=this.getDataPermission(request);
        Page<ClassQuestionnaireRecord> classQuestionnaireRecordPage = classQuestionnaireRecordService.findRecordByQueryVo(dataPermission,questionnaireRecordQueryVo, pageNo, pageSize);
        return ReturnResult.success(classQuestionnaireRecordPage) ;
    }

    @ApiOperation("班级录入列表")
    @RequestMapping(value = "/findClassExpandDetailList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ClassExpandDetailVo>> findClassExpandDetailList(ClassExpandQueryVo classExpandQueryVo,CourseClassQueryVo courseClassQueryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {

        DataPermission dataPermission = getDataPermission(request);
        courseClassQueryVo.setStatusList(Arrays.asList(1,3));
        Page<ClassExpandDetailVo> page = classExpandDetailService.findClassExpandDetail(classExpandQueryVo,courseClassQueryVo ,dataPermission , pageNo, pageSize);
        return ReturnResult.success(page);
    }


    @ApiOperation("导出班级录入列表")
    @RequestMapping(value = "/exportClassExpandDetailList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> exportClassExpandDetailList(ClassExpandQueryVo classExpandQueryVo,CourseClassQueryVo courseClassQueryVo,HttpServletRequest request, HttpServletResponse response) throws IOException{
        DataPermission dataPermission=getDataPermission(request);
        courseClassQueryVo.setStatusList(Arrays.asList(1,3));
        Webs.writeExcel(request,response, classExpandDetailService.exportClassExpandDetail(classExpandQueryVo,courseClassQueryVo,dataPermission), "班级录入情况列表");
        return ReturnResult.success("ok") ;
    }


    @ApiOperation("班级问卷结果详情")
    @RequestMapping(value = "/questionnaireRecordDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<QuestionnaireRecordDetail>> questionnaireRecordDetail(@RequestParam(value = "classQuestionnaireRecordId") long classQuestionnaireRecordId) {
        QuestionnaireRecordDetail questionnaireRecordDetail = classQuestionnaireRecordService.findDetailById(classQuestionnaireRecordId);
        return ReturnResult.success(questionnaireRecordDetail) ;
    }

    @ApiOperation("作废班级问卷结果")
    @RequestMapping(value = "/invalidQuestionnaireRecordDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> invalidQuestionnaireRecordDetail(@RequestParam(value = "classQuestionnaireRecordId") long classQuestionnaireRecordId) {
        classQuestionnaireRecordService.invalidDetail(classQuestionnaireRecordId);
        return ReturnResult.success("ok") ;
    }

    @ApiOperation("导出班级问卷结果")
    @RequestMapping(value = "/exportQuestionnaireRecord", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> exportQuestionnaireRecord(HttpServletRequest request, HttpServletResponse response,@ModelAttribute QuestionnaireRecordQueryVo questionnaireRecordQueryVo) throws IOException{
        if (questionnaireRecordQueryVo.getEndDate()!=null){
            questionnaireRecordQueryVo.setEndDate(TimeUtils.changeOneDateLastTime(questionnaireRecordQueryVo.getEndDate()));
        }
        DataPermission dataPermission=this.getDataPermission(request);
        Webs.writeExcel(request,response, classQuestionnaireRecordService.exportQuestionnaireRecord(dataPermission,questionnaireRecordQueryVo), "班级问卷结果");
        return ReturnResult.success("ok") ;
    }

    @ApiOperation("导出班级问卷结果详情")
    @RequestMapping(value = "/exportQuestionnaireRecordDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> exportQuestionnaireRecordDetail(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "classQuestionnaireRecordId")long classQuestionnaireRecordId) throws IOException{

        Webs.writeExcel(request,response, classQuestionnaireRecordService.exportQuestionnaireRecordDetail(classQuestionnaireRecordId), "班级问卷结果详情");
        return ReturnResult.success("ok") ;
    }

}
